package vn.sunnet.game.SinhTon.screen;

import java.util.ArrayList;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.OverlapTester;
import vn.sunnet.game.SinhTon.global.Setting;
import vn.sunnet.game.SinhTon.object.StageGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

// Man hinh chon chang 
public class StageScreen extends BaseScreen {
	private Button btnBack, btnTiepTuc;
	private ArrayList<StageGame> arrStageGame;
	private int optionStage = 1;
	private boolean isInvisible;
	private float x_TouchDown, y_TouchDown;
	private int stageCurrent;

	// Dung de ve ra cac hinh vuong bao doi tuong(dung de test)
	// private ShapeRenderer renderer;
	public StageScreen(final MyGame game) {
		super(game);
		if (!Assets.GET_STAGE)
			Assets.getStage();

		initButton();
		arrStageGame = new ArrayList<StageGame>();
		for (int i = 0; i < 6; i++) {
			arrStageGame.add(new StageGame(i + 1, !Setting.getBoolean("STAGE"
					+ Assets.map + "" + (i + 1))));
		}

		// chặng cao nhat ko bi khoa thi se co 1 ảnh chay len chay xuong
		for (int j = 0; j < 6; j++) {
			if (Setting.getBoolean("STAGE" + Assets.map + "" + (j + 1)))
				stageCurrent = j + 1;
		}
		if (!arrStageGame.get(stageCurrent - 1).isLock()) {
			Image img = new Image(Assets.trImage1);
			float x = arrStageGame.get(stageCurrent - 1).getBounds().x + 16;
			float y = arrStageGame.get(stageCurrent - 1).getBounds().y + 16;
			img.setPosition(x, y);
			// Co the lam mo anh nay voi dong lenh
			// img.setColor(1, 1, 1, 0.5f);

			// forever: để chạy liên tục,
			// sequence la chay lan luot tung hanh dong
			img.addAction(Actions.forever(Actions.sequence(
					Actions.moveTo(x, y + 94, 6), Actions.moveTo(x, y, 6))));
			stage.addActor(img);
		}
		Assets.stage = stageCurrent;
		// renderer = new ShapeRenderer();
	}

	@Override
	public void render(float time) {
		// super.render(time);
		stage.act(time);
		Gdx.gl.glClearColor(0.5f, 0.6f, 0.25f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		camera.update();
		stage.act(time);
		batch.begin();
		batch.draw(Assets.ttBgStage, 0, 0);

		Assets.font.draw(batch, "Chặng " + String.valueOf(Assets.stage), 345,
				469);

		for (StageGame sg : arrStageGame) {
			batch.draw(sg.getTexture(), sg.getBounds().x, sg.getBounds().y,
					sg.getBounds().width, sg.getBounds().height);
		}

		if (isInvisible) {
			batch.setColor(0.7f, 0.7f, 0.7f, 0.7f);
			batch.draw(Assets.trTiepTuc, 643, 18, 143, 45);
			batch.setColor(Color.WHITE);
		}

		batch.end();

		// renderDebug();
		update(time);
		stage.draw();

	}

	// private void renderDebug() {
	// renderer.setProjectionMatrix(camera.combined);
	// renderer.begin(ShapeType.Rectangle);
	//
	// for (StageGame bo : arrStageGame) {
	// renderer.rect(bo.getBounds().x, bo.getBounds().y,
	// bo.getBounds().width, bo.getBounds().height);
	// }
	//
	// renderer.end();
	// }

	private void update(float time) {
		updateTouchAreaStage(time);
		updateStage(time);
	}

	private void updateTouchAreaStage(float time) {
		for (StageGame sg : arrStageGame) {
			if (OverlapTester.pointInRectangle(sg.getBounds(), x_TouchDown,
					y_TouchDown)) {
				optionStage = sg.getNumberStage();
				Assets.stage = sg.getNumberStage();
			}
		}
	}

	private void updateStage(float time) {
		if (optionStage > Assets.stage) {
			isInvisible = true;
			btnTiepTuc.remove();
		} else {
			isInvisible = false;
			stage.addActor(btnTiepTuc);
		}
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		super.dispose();
		arrStageGame.clear();
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.BACK)
			game.setScreen(new SelectMap(game));
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return super.keyUp(keycode);
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		x_TouchDown = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_TouchDown = (Gdx.graphics.getHeight() - screenY)
				* Assets.SCREEN_HEIGHT / Gdx.graphics.getHeight();
		// Neu touchDown vao anh cac man da duoc mo khoa thi chuyen man hinh
		// ShopGun
		for (StageGame sg : arrStageGame) {
			if (!sg.isLock()) {
				if (OverlapTester.pointInRectangle(sg.getBounds(), x_TouchDown,
						y_TouchDown)) {
					optionStage = sg.getNumberStage();
					Assets.stage = sg.getNumberStage();
					game.setScreen(new ShopGun(game));
				}
			}
		}
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return super.touchUp(screenX, screenY, pointer, button);
	}

	private void initButton() {
		btnBack = new Button(Assets.skin, "btnBack");
		btnBack.setBounds(14, 18, 77, 45);
		// btnBack.setBounds(389, 16, 123, 73);
		btnBack.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.request.trackEndLevel(Assets.map, Assets.score);
				game.setScreen(new SelectMap(game));
			}
		});
		stage.addActor(btnBack);

		// Nut tiep theo
		btnTiepTuc = new Button(Assets.skin, "btnTiepTuc");
		btnTiepTuc.setBounds(643, 18, 143, 45);
		btnTiepTuc.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				if (!isInvisible)
					game.setScreen(new ShopGun(game));
			}
		});
		// stage.addActor(btnTiepTuc);
	}
}
